<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{baseui/head :: head}">
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="动检通智慧云平台"/>
    <meta name="author" content="牧中(上海)物联网科技有限公司"/>
    <link rel="shortcut icon" href="/webjars/images/favicon.png"/>
    <link rel="stylesheet" href="/webjars/js/layui/css/layui.css" media="all">
    <title>牧中畜牧业大数据智慧云平台V1.0</title>


</head>
<body>
<!--顶部一级导航-->
<th:block th:replace="baseui/topheader :: topheader(1)"/>


<!--平台管理菜单-->
<div id="topnav" th:replace="djt/partial/topnav :: topnav(8)"></div>


<div id="cl-wrapper">
    <!--左边导航-->
    <th:block th:replace="djt/partial/low_left :: nav(21)"/>

    <div class="container-fluid" id="pcont">
        <!--<div class="page-head">-->
        <!--<h3>生产记录</h3>-->
        <!--</div>-->


        <div class="row">
            <div class="col-md-12">
                <div class="block-flat">
                    <div class="header"><h3>当场处罚</h3></div>
                    <div class="content">
                        <div class="table-responsive">
                            <div id="datatable_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">


                                <div class="row">

                                    <!-- search-->

                                    <div style="white-space: nowrap">



                                        <span style="margin-left: 50px">   区域:
                                                           <select class="select2" style="max-width: 120px;"
                                                                   onchange="showfactory()" id="city">
                                                           <option value="全部">所有区域</option>
                                                               <span th:each="cityAreaDto :${cityAreaDtoList}">
                                                            <optgroup th:label="${cityAreaDto.city}">
                                                                <span th:each="region :${cityAreaDto.regions}">
                                                            <option th:value="${region}"
                                                                    th:text="${region}">南漳县</option>
                                                                    </span>
                                                          </optgroup>
                                                                   </span>
                                                                 </select>

                                            </span>

                                        <span style="margin-left: 50px">   案由:
                                                           <select class="select2" style="max-width: 100px;"
                                                                   >
                                                               <option value="-1">所有案由</option>


                                                                 </select>

                                            </span>

                                        <span style="margin-left: 50px">   来源:


                                                           <select class="select2" style="max-width: 160px;"
                                                                   >

                                                                       <option value='-1' selected>所有来源</option>



                                                       </select>

                                                         </span>

                                        <span style="margin-left: 50px">   案件类型:


                                                           <select class="select2" style="max-width: 160px;"
                                                                   >

                                                                       <option value='-1' selected>所有类型</option>



                                                       </select>

                                                         </span>

                                        <span style="margin-left: 50px">
                                                <button class="btn btn-primary" onclick="SearchBtn()">查询</button>
                                                </span>


                                        <span style="margin-right:50px;">
                                                <button th:if="${userProfile!=null}" class="btn btn-primary"  >增加养殖场</button>
                                                </span>


                                    </div>


                                    <!--
                                                                            <div class="col-sm-6">
                                                                            <div id="datatable_filter" class="dataTables_filter">
                                                                            <label>区域:<select class="select2">
                                                                                <option value="所有区域">所有区域</option>
                                                                                <optgroup label="襄阳市">
                                                                                    <option value="南漳县">南漳县</option>
                                                                                    <option value="其它县">其它县</option>
                                                                                </optgroup>
                                                                            </select>


                                                                            </label></div>
                                                                            </div>
                                                                        </div>
                                    -->
                                    <div class="row">
                                        <div class="col-sm-12">

                                            <!-- Table -->
                                            <table id="jqGrid"></table>
                                            <div id="jqGridPager"></div>

                                        </div>
                                    </div>


                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


        </div>
    </div>

</div>

<script type="text/javascript" src="/webjars/js/jquery.js"></script>
<script type="text/javascript" src="/webjars/js/jquery.gritter/js/jquery.gritter.js"></script>

<script type="text/javascript" src="/webjars/js/jquery.nanoscroller/jquery.nanoscroller.js"></script>
<script type="text/javascript" src="/webjars/js/behaviour/general.js"></script>
<script src="/webjars/js/jquery.ui/jquery-ui.js" type="text/javascript"></script>

<script type="text/javascript" src="/webjars/js/jquery.nestable/jquery.nestable.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.switch/bootstrap-switch.min.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script src="/webjars/js/jquery.select2/select2.min.js" type="text/javascript"></script>
<script src="/webjars/js/skycons/skycons.js" type="text/javascript"></script>
<script src="/webjars/js/bootstrap.slider/js/bootstrap-slider.js" type="text/javascript"></script>
<script type="text/javascript" src="/webjars/js/jquery.niftymodals/js/jquery.modalEffects.js"></script>
<script type="text/javascript" src="/webjars/js/bootstrap.summernote/dist/summernote.min.js"></script>


<script type="text/javascript" src="/webjars/js/jquery.magnific-popup/dist/jquery.magnific-popup.min.js"></script>


<script src="/webjars/js/behaviour/voice-commands.js"></script>
<script src="/webjars/js/bootstrap/dist/js/bootstrap.min.js"></script>

<script type="text/ecmascript" src="/webjars/jqgrid/js/i18n/grid.locale-cn.js"></script>
<script type="text/ecmascript" src="/webjars/jqgrid/js/jquery.jqGrid.min.js"></script>
<script src="/webjars/js/laydate/laydate.js"></script>
<script src="//vuejs.org/js/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
<script src="/webjars/js/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    var PostDatas = {
        };
    $(document).ready(function () {

        App.init();

        //grid
        $("#jqGrid").jqGrid({
            url: '/management/factory/showList',
            postData: PostDatas,
            mtype: "GET",
            styleUI: 'Bootstrap',
            datatype: "json",
            autowidth: true,
            colModel: [
                {label: '序号', name: 'breedFactory.id', key: true, width: 100, align: 'center'},
                {label: '<span style="text-align: center">来源</span>', name: 'breedFactory.name', width: 200, align: 'center'},
                {label: '<span style="text-align: center">案由</span>', name: 'user.name', width: 100, align: 'center'},
                {label: '<span style="text-align: center">案件类型</span>', name: 'user.mobile', width: 150, align: 'center'},
                {label: '<span style="text-align: center">当事人姓名</span>', name: 'breedFactory.detail', width: 150, align: 'center'},
                {label: '<span style="text-align: center">电话</span>', name: 'breedFactory.isScale', width: 100, align: 'center',formatter:showisScale},
                {label: '<span style="text-align: center">操作</span>', width: 200, formatter: ShowOpertator, align: 'center'}
            ],
            viewrecords: true,
            align: 'center',
            height: 350,
            rowNum: 20,
            pager: "#jqGridPager"
        });

    });
    function ShowDate(cellvalue, options, rowObject) {
        if (cellvalue != null && cellvalue != "") {
            var timestamp4 = new Date(cellvalue);
            var dt = timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8);
            return dt;
        } else {
            return "";
        }
    }


    function SearchBtn() {
        var region = $("#region").val();
        var name = $("#name").val();

        var sdata = {   // 构建查询需要的参数
            breedFactoryId: factoryoption(),
            isScale:1
        };

        var postData = $("#jqGrid").jqGrid("getGridParam", "postData");

        $.extend(postData, sdata);

        $("#jqGrid").jqGrid("setGridParam", {
            search: true    // (6)将jqGrid的search选项设为true
        }).trigger("reloadGrid", [{page: 1}]);   // (7)重新载入Grid表格，以使上述设置生效

    }
    function ShowBreeType(cellvalue, options, rowObject) {
        var type;
        switch (cellvalue) {
            case 1:
                type = "猪";
                break;
            case 2:
                type = "牛";
                break;
            case 3:
                type = "羊";
                break;
            case 4:
                type = "家禽";
                break;
        }
        return type;
    }
    function showisScale(cellvalue, options, rowObject) {
        switch (cellvalue){
            case 1:
                return "规模场";
                break;
            case 0:
                return "散户";
                break;
        }
    }
    function ShowOpertator(cellvalue, options, rowObject) {
        var link;
        var row = JSON.stringify(rowObject);
        link = "<button type='button' class='btn btn-default' id='" + row + "' onclick='select(this.id)'>详细信息</button><button type='button' class='btn btn-default' id='" + row + "' onclick='update(this.id)'>编辑</button>";
        return rowObject.id == -1 ? "" : link;
    }


    function select(id){

    }
   /* function select(data) {
        var json = JSON.parse(data);
        var type = showtype(json.harmless.animalType, null, null);
        var method = showmethod(json.harmless.treatment, null, null);
        var local = showlocal(json.harmless.isLocal, null, null);
        var timestamp4 = new Date(json.harmless.createdAt);
        var dt = timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8);
        var unit = shownum(json.harmless.quantity, null, json);
        layui.use(['laypage', 'layer'], function () {
            var $ = layui.jquery, layer = layui.layer;
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                area: ['800px', '600px'],
                content: ['/djt/harmless/harmlessDetail', 'no'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    body.find("#factory").text(json.harmless.breedFactoryName);
                    body.find("#factorynum").text(json.breedFactory.id);
                    body.find("#region").text(json.breedFactory.region);
                    body.find("#stable").text(json.breedFactory.stableNumber);
                    body.find("#reportname").text(json.harmless.applyReportName);
                    body.find("#butchername").text(json.harmless.butcherFactoryName);
                    body.find("#protype").text(type);
                    body.find("#num").text(unit);
                    body.find("#treatment").text(method);
                    body.find("#quarantinename").text(json.harmless.quarantineUserName);
                    body.find("#islocal").text(local);
                    body.find("#createat").text(dt);


                    var tt = json.harmless.photosJsonMap;

                    for (var key in tt) {
                        if (tt[key] != null) {
                            var a = "<img src=" + tt[key]['uri'] + " class='img-thumbnail' layer-src=" + tt[key]['uri'] + " style='width: 150px;height: 200px' onclick='showimg(this)'>";
                            if (tt[key]['uri'] != null && tt[key]['uri'] != "") {
                                body.find("#photosJson").append(a);
                            }
                        }


                    }

                }
            });

        });
    }*/



    function factoryoption() {

        var factory_id = $("#factory").val();


        if (factory_id == -1) {
            var array1 = new Array();
            $("#factory option").each(function () {  //遍历所有option
                var txt = $(this).val();   //获取option值
                if (txt != '') {
                    array1.push(txt);  //添加到数组中
                }
            });

            return array1.toString();
        } else if (factory_id == 0) {
            var array3 = new Array();
            array3.push(factory_id);
            return array3.toString();

        } else {
            var array2 = new Array();
            array2.push(factory_id);
            return array2.toString();
        }

    }

    function city() {

        var city = $("#city").val();


        if (city =="全部") {
            var array1 = new Array();
            $("#city option").each(function () {  //遍历所有option
                var txt = $(this).val();   //获取option值
                if (txt != '') {
                    array1.push(txt);  //添加到数组中
                }
            });

            return array1.toString();
        }else {
            var array2 = new Array();
            array2.push(city);
            return array2.toString();
        }

    }

    function showfactory() {



        var type = $("#breedType").val();

        $.ajax({

            url: "/djt/breedarch/search",
            data: {region: city(), breedType: type},
            dataType: "json",
            success: function (data) {

                $("#factory").empty();

                if (data.length != 0) {
                    var str1 = " <option value='-1' selected>所有养殖场</option>";
                    $("#factory").append(str1);
                    $.each(data, function (index, obj) {
                        var id = obj.id;
                        var name = obj.name;
                        var str = "<option value=" + id + ">" + name + "</option>";

                        $("#factory").append(str);
                    });
                } else {

                    var str = "<option value='0' selected>无</option>";
                    $("#factory").append(str);
                }
            }
        });
    }


    function select(data) {
        var json = JSON.parse(data);
     var breedType=ShowBreeType(json.breedFactory.breedType,null,null);
        layui.use(['laypage', 'layer'], function () {
            var $ = layui.jquery, layer = layui.layer;
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                area: ['800px', '600px'],
                content: ['/management/factory/select', 'no'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];

                    body.find("#breedFactoryName").text(json.breedFactory.name);
                    body.find("#name").text(json.user.name);
                    body.find("#phone").text(json.user.mobile);
                    body.find("#breedType").text(breedType);
                    body.find("#stockQuantity").text(json.breedFactory.stockQuantity);
                    body.find("#longitude").text(json.breedFactory.longitude);
                    body.find("#latitude").text(json.breedFactory.latitude);
                    body.find("#detail").text(json.breedFactory.detail);


                }
            });

        });
    }

    function update(data) {
        var json = JSON.parse(data);
        var breedType=ShowBreeType(json.breedFactory.breedType,null,null);
        layui.use(['laypage', 'layer'], function () {
            var $ = layui.jquery, layer = layui.layer;
            layer.open({
                type: 2,
               // skin: 'layui-layer-lan',
                area: ['800px', '500px'],
                title:'更改信息',
                content: ['/management/factory/update', 'no'],
                btn:['更改','取消'],
                btn1: function(index, layero){
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    var data= {
                        id: json.breedFactory.id,
                        name: body.find("#breedFactoryName").val(),
                        username: body.find("#name").val(),
                        phone: body.find("#phone").val(),
                        breedType: json.breedFactory.breedType,
                        stockQuantity: body.find("#stockQuantity").val(),
                        longitude: body.find("#longitude").val(),
                        latitude: body.find("#latitude").val(),
                        detail: body.find("#detail").val()
                    }
                    layer.msg('你确定要更改养殖场信息吗？', {
                        time: 60000, //20s后自动关闭
                        btn: ['确定','取消' ],
                        btnAlign: 'c',
                        btn1:function(index, layero){
                            $.ajax({

                                url: "/management/factory/update1",
                                data:data,
                                dataType: "json",
                                success: function (data) {


                                          if (data){
                                              layer.msg('修改成功',{btn: '确定' ,time: 60000,btnAlign: 'c',area: ['150px', '100px'],
                                              btn1:function(index, layero){
                                                  window.location.href="/management/factory/show";
                                              }}) ;

                                          }else{
                                              layer.msg('修改失败',{btn: '确定' ,time: 60000,btnAlign: 'c',area: ['150px', '100px']}) ;
                                          }

                                }

                            });

                        }

                    });

                },
                success: function (layero, index) {

                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];

                    body.find("#breedFactoryName").val(json.breedFactory.name);
                    body.find("#name").val(json.user.name);
                    body.find("#phone").val(json.user.mobile);
                    var ber=ShowBreeType(json.breedFactory.breedType,null,null);
                    body.find("#breedType").val(ber);
                    body.find("#stockQuantity").val(json.breedFactory.stockQuantity);
                    body.find("#longitude").val(json.breedFactory.longitude);
                    body.find("#latitude").val(json.breedFactory.latitude);
                    body.find("#detail").val(json.breedFactory.detail);


                },
            });

        });
    }

    function add(){


        layui.use(['laypage', 'layer'], function () {
            var $ = layui.jquery, layer = layui.layer;
            layer.open({
                type: 2,
                // skin: 'layui-layer-lan',
                area: ['800px', '500px'],
                title:'增加养殖场',
                content: ['/management/factory/add', 'no'],
                btn:['增加','取消'],
                btn1: function(index, layero){
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];


                    var data= {
                        village:body.find("#city").val(),
                        name: body.find("#breedFactoryName").val(),
                        username: body.find("#name").val(),
                        phone: body.find("#phone").val(),
                        breedType: body.find("#breedType").val(),
                        stockQuantity: body.find("#stockQuantity").val(),
                        longitude: body.find("#longitude").val(),
                        latitude: body.find("#latitude").val(),
                        detail: body.find("#detail").val(),
                        isScale:1,
                        password:body.find("#password").val()
                    }
                    var a=false;
               for (var key in data){
                  if (data[key]==null||data[key]==""){
                      a=true;
                      break;
                  }
               }

  if (!a) {
      layer.msg('请再次查看信息是否有误', {
          time: 60000, //20s后自动关闭
          btn: ['确定', '取消'],
          btnAlign: 'c',
          btn1: function (index, layero) {
              $.ajax({

                  url: "/management/factory/add1",
                  data: data,
                  dataType: "json",
                  success: function (data) {


                      if (data == 1) {
                          layer.msg('增加养殖场成功', {
                              btn: '确定', time: 60000, btnAlign: 'c', area: ['150px', '100px'],
                              btn1: function (index, layero) {
                                  window.location.href = "/management/factory/show";
                              }
                          });

                      } else {
                          layer.msg('增加养殖场失败', {btn: '确定', time: 60000, btnAlign: 'c', area: ['150px', '100px']});
                      }

                  }

              });

          }

      });


  }else{
      body.find("#message").text("表单不能为空");
  }


                }

            });

        });

    }

</script>


</body>
</html>
